package com.xw.db137.controller;

import com.xw.db137.common.R;
import com.xw.db137.entity.Role;
import com.xw.db137.service.RoleService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: xw
 * @CreateTime: 2024/12/24
 * @Description: 用户
 * @Version: 1.0
 */
@RestController
@CrossOrigin(origins = "*")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 获取所有的角色集合
     * @return
     */
    @GetMapping("/selectAllRole")
    public R selectAllRole() {
        List<Role> list = roleService.list();
        return new R().addData("list", list);
    }

    /**
     * 新增
     * @param rolename
     * @param description
     * @return
     */
    @PostMapping("/inserRole/{rolename}/{description}")
    public R inserRole(@PathVariable("rolename")String rolename,@PathVariable("description")String description) {
        Role role = new Role();
        role.setRolenane(rolename);
        role.setDescription(description);
        roleService.save(role);
        return new R();
    }

    /**
     * 修改
     * @param role
     * @return
     */
    @PostMapping("/updateRole")
    public R updateRole(@RequestBody Role role) {
        roleService.updateById(role);
        return new R();
    }

    /**
     * 根据用户id获取用户拥有的角色集合
     * @param userId
     * @return
     */
    @GetMapping("/selectRoleByUserId/{userId}")
    public R selectRoleByUserId(@PathVariable("userId") int userId) {
        List<Role> rolelist= this.roleService.selectRoleByUserId(userId);
        return new R().addData("rolelist",rolelist);
    }
}
